package com.example.styservice.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.styinterface.interfaces.IRedisService;
import com.example.styinterface.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
@Service
public class RedisServiceImpl implements IRedisService {
    @Autowired
    private RedisTemplate redisTemplate;

    public R insert(String key, String value) {

        ValueOperations opsForValue = redisTemplate.opsForValue();
        opsForValue.set(key, value);
        return R.success(200, "添加成功");
    }

    public R delete(String key) {
        if (!redisTemplate.hasKey(key)) {
            return R.success(200, "该key无信息");
        }
        redisTemplate.delete(key);
        return R.success(200, "删除成功");
    }

    public R selectKey(String key) {
        if (!redisTemplate.hasKey(key)) {
            return R.success(200, "该key无信息");
        }
        ValueOperations opsForValue = redisTemplate.opsForValue();
        String string = opsForValue.get(key).toString();
        return R.success(string, 200, "获取成功");

    }
}
